package com.example.childpositionapp;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.childpositionapp.model.Location;
import com.example.childpositionapp.model.Login;
import com.example.childpositionapp.model.Receive;
import com.example.childpositionapp.network.Tool;
import com.example.childpositionapp.network.Util;
import com.example.childpositionapp.util.UtilMd5;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegisterActivity extends AppCompatActivity {
    EditText et_yhm;
    EditText et_phone;
    EditText et_age;
    EditText et_mm;
    EditText et_qrmm;
    RadioGroup radioGroup;
    Button bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        et_age = findViewById(R.id.et_yx);
        et_yhm = findViewById(R.id.et_yhm);
        et_phone = findViewById(R.id.et_sjh);
        et_mm = findViewById(R.id.et_mm);
        et_qrmm = findViewById(R.id.et_qrmm);
        radioGroup = findViewById(R.id.rg_gender);
        bt = findViewById(R.id.bt_register);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("".equals(et_yhm.getText().toString())) {
                    Toast.makeText(RegisterActivity.this, "用户名不能为空！", Toast.LENGTH_SHORT).show();
                } else if ("".equals(et_phone.getText().toString())) {
                    Toast.makeText(RegisterActivity.this, "邮箱不能为空！", Toast.LENGTH_SHORT).show();
                } else if ("".equals(et_age.getText().toString())) {
                    Toast.makeText(RegisterActivity.this, "年龄不能为空！", Toast.LENGTH_SHORT).show();
                } else if (!et_mm.getText().toString().equals(et_qrmm.getText().toString())) {
                    Toast.makeText(RegisterActivity.this, "两遍密码不一样！", Toast.LENGTH_SHORT).show();
                } else {
                    String s = null;
                    switch (radioGroup.getCheckedRadioButtonId()) {
                        case R.id.rb_male:
                            s = "男";
                            break;
                        case R.id.rb_female:
                            s = "女";
                            break;
                    }
                    String age=et_age.getText().toString();
                    int age1= Integer.parseInt(age);
                    String text = new Gson().toJson(new Login(et_phone.getText().toString(), UtilMd5.stringMD5(et_mm.getText().toString()),et_yhm.getText().toString()));
                    sendRegister(text);
                }
            }
        });
    }

    private void sendRegister(String text) {
        Tool.Register(text, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(RegisterActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String s=response.body().string();

                Receive receive = new Gson().fromJson(s,Receive.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (receive.getCode() == 200) {
                            Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Log.d("register",s);
                            Toast.makeText(RegisterActivity.this, receive.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}